using Vwf.Web;
using Vwf.Tools;
using Gee;

namespace Vwf.Web.Controls {
	public class HtmlTag : Control {
		public string tagName = "";
		
		// Render tag in form <tagName /> or <tagName></tagName>
		// true - if the element does not contain items used <tagName />. Otherwise, the <tagName></tagName>
		// false - always use <tagName></tagName>
		// null - automatically selects the best option
		public bool? single = null
		
		public static string autoSingle = new string[] { "img", "br", "link", "meta", "hr", "input", "area" };
		
		protected bool isSingle() {
			if ((single == false) || (items.size > 0)) return false;
			if (single == true) return true;
			return tagName.lowercase() in autoSingle;
		}
		
		public HashMap<string, string> attribs = new HashMap<string, string>();
	
		public override void render(Writer writer) {
			
			writer.write("<" + tagName);
			foreach(var item in attribs) {
				writer.write(" " + item.key + "=\"" + Tools.Html.EncodeHtml(item.value) + "\"")
			};
			if (isSingle()) {
				writer.write(" />");
			} else {
				writer.write(">");
				content.render(writer);
				writer.write("</" + tagName + ">");
			};
		}
	}
}
